//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include "DtwGame.h"


//------------------------------------------------------------------------------
//=== constructor ===//
DtwDatabase::DtwDatabase()
{
}
//------------------------------------------------------------------------------
//=== destructor ===//
DtwDatabase::~DtwDatabase()
{
}
//------------------------------------------------------------------------------
//! startup
RCODE DtwDatabase::DbStartup( const char * pDbFileName )
{
	return DbBase::DbStartup( pDbFileName );
}

//------------------------------------------------------------------------------
//! shutdown
RCODE DtwDatabase::DbShutdown( void )
{
	return DbBase::DbShutdown();
}

//------------------------------------------------------------------------------
//! create initial tables in the database ( derived classes should override )
RCODE DtwDatabase::DbCreateTables( void )
{
	RCODE rc;
	rc = DbOpen();
	if( rc )
	{   
		log_msg( 0, "DtwDatabase::DbCreateTables eroor %d\n", rc );
		return rc;
	}
	RunQuery("PRAGMA journal_mode = 'OFF'");
	RunQuery("BEGIN TRANSACTION");

	rc |= RunQuery( "CREATE TABLE DatabaseInfo('Version' INTEGER)" );
	if( SQLITE_OK != rc )
	{
		DbClose();
		log_msg( 0, "DtwDatabase::Create DatabaseInfo table eroor %d\n", rc );
		vx_assert( false );
		return -1;
	}
	rc |= RunQuery(	"CREATE TABLE Stats("
						"'ID' INTEGER PRIMARY KEY"
						", 'LevelName' TEXT"
						", 'Difficulty' INTEGER DEFAULT(0)"
						", 'Unlocked' INTEGER DEFAULT(0)"
						", 'PlayTimeMs' INTEGER DEFAULT(0)"
						", 'WinCount' INTEGER DEFAULT(0)"
						", 'LoseCount' INTEGER DEFAULT(0)"
						", 'LoadCount' INTEGER DEFAULT(0)"
						")");			
	rc |= RunQuery(	"CREATE TABLE HighScores("
						"'ID' INTEGER PRIMARY KEY"
						", 'StatsId' INTEGER DEFAULT(0)"
						", 'PlayTimeMs' INTEGER"
						", 'UnixDate' INTEGER"
						")");
	rc |= RunQuery("CREATE INDEX StatsLevelName on Stats (LevelName ASC)");

	// set version number
	char as8Buffer[ 256 ];
	sprintf( as8Buffer, "INSERT INTO DatabaseInfo(Version) VALUES(%d)", DTW_DATABASE_VERSION );
	rc |= RunQuery( as8Buffer );

	rc |= RunQuery("END TRANSACTION");


	DbClose();
	return rc;
}
//------------------------------------------------------------------------------
//! get database version.. return 0 if failed
S32 DtwDatabase::GetDatabaseVersion( void )
{
	RCODE rc;
	if( rc = DbOpen() )
	{
		log_msg( 0, "DtwDatabase::GetDatabaseVersion: DbOpen Failed error $d\n", rc );
		return 0;
	}
	StartDataQuery("SELECT Version from DatabaseInfo");
	GetDbRow();
	S32 s32Version = GetDbS32(0);
	CloseQuery();
	DbClose();
	return s32Version;
}
